package com.zh.risk.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.risk.dao.BlackListDao;
import com.zh.risk.entity.RiskBlackList;
import com.zh.risk.service.BlackListService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description:
 * @Author: Liard
 * @Create in: 2020/9/18 14:36
 * @Version: 1.0.1
 */
@Service
public class BlackListServiceImpl extends ServiceImpl<BlackListDao, RiskBlackList> implements BlackListService {

    @Autowired
    private BlackListDao dao;

    @Override
    public IPage<RiskBlackList> getBlackMrcList(IPage<RiskBlackList> page, RiskBlackList param) {
        return dao.selectPage(page, new LambdaQueryWrapper<RiskBlackList>()
                .eq(RiskBlackList::getType, param.getVal())
                .like(StrUtil.isNotEmpty(param.getDescription()), RiskBlackList::getDescription, param.getDescription())
                .like(StrUtil.isNotEmpty(param.getRiskObj()), RiskBlackList::getRiskObj, param.getRiskObj()));
    }


    @Override
    public boolean addBlackMrcList(RiskBlackList param) { return dao.insert(param) > 0; }

    @Override
    public int modBlackMrcList(RiskBlackList param) {
        return dao.update(param, new LambdaQueryWrapper<RiskBlackList>().eq(RiskBlackList::getId, param.getId()));
    }

    @Override
    public int delBlackMrcInfo(RiskBlackList param) {
        return dao.delete(new LambdaQueryWrapper<RiskBlackList>().eq(RiskBlackList::getId, param.getId()));
    }

    @Override
    public int delBatBlackMrcInfo(List<Integer> param) { return dao.deleteBatchIds(param); }

    @Override
    public boolean addBlackMrcListByExcel(MultipartFile file) {
        boolean reslut =false;
        if(file.isEmpty()) return false;
        String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."), file.getOriginalFilename().length());
        if(!Objects.equals(fileType,".xlsx") || !Objects.equals(fileType,".xls"))return false;
        try{
            List<RiskBlackList> riskBlackLists = new ArrayList<RiskBlackList>();
            Workbook workbook = null;
            workbook = WorkbookFactory.create(file.getInputStream());
            file.getInputStream().close();
            //获取所有的工作表的的数量
            int numOfSheet = workbook.getNumberOfSheets();
            //遍历这个这些表
            for (int i = 0; i < numOfSheet; i++) {
                //获取一个sheet也就是一个工作簿
                Sheet sheet = workbook.getSheetAt(i);
                int lastRowNum = sheet.getLastRowNum();
                //从第一行开始第一行一般是标题
                for (int j = 1; j <= lastRowNum; j++) {
                    Row row = sheet.getRow(j);
                    //获取单元格的值
                    String code = row.getCell(0).getStringCellValue();
                    String name = row.getCell(1).getStringCellValue();
                    String type = row.getCell(2).getStringCellValue();
                    RiskBlackList riskBlackList = new RiskBlackList();
                    riskBlackList.setType(Integer.valueOf(type));
                    riskBlackList.setDescription(name);
                    riskBlackList.setRiskObj(code);
                    riskBlackLists.add(riskBlackList);
                    //System.err.println("地区编码："+code +" 地区名称: "+name);
                }
            }
            //批量新增
            if (riskBlackLists.size()>0) {
                reslut= this.saveBatch(riskBlackLists, riskBlackLists.size());
            }
        }catch (IOException e){
            e.printStackTrace();
        }
        return  reslut;
    }
}
